package com.common.db;

//=UPPER(LEFT( A1, 1 ))&RIGHT( A1, LEN(A1)-1 )
import java.sql.ResultSet;

import com.common.shared.ClSelectionItem;
import com.common.shared.map.GisLayer;
import com.common.shared.map.GisLayerFilter;
import com.common.shared.map.GisLayerOptions;
import com.common.shared.map.GisMap;
import com.common.shared.map.GisMapClickedObject;
import com.common.shared.map.GisMapProperties;
import com.common.shared.map.GisMap_Building;
import com.common.shared.map.GisMap_Roads;
import com.common.shared.map.MLParameter;
import com.common.shared.model.Classifier;
import com.common.shared.model.ClassifierType;
import com.common.shared.usermanager.Group;
import com.common.shared.usermanager.GroupPermition;
import com.common.shared.usermanager.Permission;
import com.common.shared.usermanager.PermitionTable;
import com.common.shared.usermanager.User;
import com.common.shared.usermanager.UserGroup;
import com.common.shared.usermanager.UserPermition;

public class DBClassMapping {

	public static Classifier getClassifier(ResultSet rs) throws Exception {
		Classifier cl = new Classifier();
		cl.setId(rs.getInt("id"));
		cl.setCltype(rs.getInt("cltype"));
		cl.setTypeid(rs.getInt("typeid"));
		cl.setTypename(rs.getString("typename"));
		return cl;
	}

	public static ClassifierType getClassifierType(ResultSet rs)
			throws Exception {
		ClassifierType cl = new ClassifierType();
		cl.setId(rs.getInt("id"));
		cl.setClassifictypename(rs.getString("classifictypename"));
		return cl;
	}

	public static User getUser(ResultSet rs) throws Exception {
		User result = new User();
		result.setId(rs.getInt("id"));
		result.setUsername(rs.getString("user_name"));

		try {
			result.setCaption_id(rs.getInt("caption_id"));
		} catch (Exception e) {
		}

		try {
			result.setPwd(rs.getString("pwd"));
		} catch (Exception e) {
		}

		try {
			result.setRegionid(rs.getInt("regionid"));
		} catch (Exception e) {
		}
		try {
			result.setSubregionid(rs.getInt("subregionid"));
		} catch (Exception e) {
		}
		return result;
	}

	public static UserGroup getUserGroup(ResultSet rs) throws Exception {
		UserGroup result = new UserGroup();
		result.setGroup_id(rs.getInt("group_id"));
		result.setUser_id(rs.getInt("user_id"));

		return result;
	}

	public static Group getGroup(ResultSet rs) throws Exception {
		Group result = new Group();
		result.setId(rs.getInt("id"));
		result.setCaption_id(rs.getLong("caption_id"));
		result.setGroup_name(rs.getString("group_name"));
		return result;
	}

	public static PermitionTable getPermitionTable(ResultSet rs)
			throws Exception {
		PermitionTable result = new PermitionTable();
		result.setPermition_id(rs.getInt("permition_id"));
		result.setUser_or_group_id(rs.getInt("user_or_group_id"));
		result.setUserperm(rs.getBoolean("userperm"));
		result.setAccepted(rs.getBoolean("accepted"));
		return result;
	}

	public static Permission getPermition(ResultSet rs) throws Exception {
		Permission result = new Permission();
		result.setId(rs.getInt("id"));
		try {
			result.setCaption_id(rs.getLong("caption_id"));
		} catch (Exception e) {
		}
		result.setPermission_name(rs.getString("permission_name"));
		return result;
	}

	public static GisMapClickedObject getGisMapClickedObject(ResultSet rs)
			throws Exception {
		GisMapClickedObject result = new GisMapClickedObject();
		result.setGid(rs.getLong("gid"));
		result.setObj_text(rs.getString("obj_text"));
		result.setGis_astext(rs.getString("gis_astext"));
		result.setObj_type(rs.getInt("obj_type"));
		result.setRaioni(rs.getLong("raioni"));
		result.setRegioni(rs.getLong("regioni"));
		result.setRegioni(rs.getLong("regioni"));
		result.setSstreet(rs.getString("sstreet"));
		result.setCity(rs.getString("city"));
		return result;
	}

	public static UserPermition getUserPermitions(ResultSet rs)
			throws Exception {
		UserPermition result = new UserPermition();
		result.setUser_id(rs.getInt("user_id"));
		result.setPermition_type(rs.getShort("permition_type"));
		result.setGroup_or_permition_id(rs.getInt("group_or_permition_id"));
		result.setAccept(rs.getBoolean("accept"));
		return result;
	}

	public static GroupPermition getUserGroupPermition(ResultSet rs)
			throws Exception {
		GroupPermition result = new GroupPermition();
		result.setGroup_id(rs.getInt("group_id"));
		result.setPermition_id(rs.getInt("permition_id"));
		result.setAccept(rs.getBoolean("accept"));
		return result;
	}

	public static ClSelectionItem getClSelectionItem(ResultSet rs,
			boolean withParent) throws Exception {

		ClSelectionItem result = new ClSelectionItem();
		result.setId(rs.getLong("id"));
		result.setValue(rs.getString("cvalue"));
		if (withParent) {
			result.setParentId(rs.getLong("parentId"));
		}
		return result;
	}

	public static ClSelectionItem getClSelectionItem(ResultSet rs)
			throws Exception {

		ClSelectionItem result = new ClSelectionItem();
		result.setId(rs.getLong("id"));
		result.setValue(rs.getString("cvalue"));
		try {
			result.setParentId(rs.getLong("parentId"));

		} catch (Exception e) {
		}
		return result;
	}

	public static GisMap_Roads getGisMap_Roads(ResultSet rs) throws Exception {
		GisMap_Roads result = new GisMap_Roads();
		setGisMap_Roads(result, rs);
		return result;
	}

	public static GisMap_Building getGisMap_Building(ResultSet rs)
			throws Exception {
		GisMap_Building result = new GisMap_Building();
		setGisMap_Roads(result, rs);
		result.setHome(rs.getString("home"));
		return result;
	}

	public static void setGisMap_Roads(GisMap_Roads result, ResultSet rs)
			throws Exception {
		result.setGid(rs.getLong("gid"));
		result.setRaioni(rs.getLong("raioni"));
		result.setRegioni(rs.getLong("regioni"));
		result.setStreet(rs.getString("street"));
		result.setCity(rs.getString("city"));
	}

	public static MLParameter getMLParameter(ResultSet rs) throws Exception {
		MLParameter mlParameter = new MLParameter();
		setMLParameter(mlParameter, rs);
		return mlParameter;
	}

	public static void setMLParameter(MLParameter mlParameter, ResultSet rs)
			throws Exception {

		mlParameter.setPropertie_name(rs.getString("propertie_name"));
		mlParameter.setPropertie_value(rs.getString("propertie_value"));
		mlParameter.setPtype(rs.getInt("ptype"));

	}

	public static GisMap getMap(ResultSet rs) throws Exception {
		GisMap res = new GisMap();
		res.setId(rs.getInt("id"));
		res.setMapname(rs.getString("mapname"));
		res.setZoomto(rs.getString("zoomto"));
		res.setWidth(rs.getString("width"));
		res.setHeight(rs.getString("height"));
		return res;
	}

	public static GisMapProperties getGisMapProperties(ResultSet rs)
			throws Exception {
		GisMapProperties res = new GisMapProperties();
		res.setId(rs.getInt("id"));
		res.setGismap_id(rs.getInt("gismap_id"));
		setMLParameter(res, rs);
		return res;
	}

	public static GisLayerOptions getGisLayerOptions(ResultSet rs)
			throws Exception {
		GisLayerOptions res = new GisLayerOptions();
		res.setId(rs.getInt("id"));
		res.setGis_layer_id(rs.getInt("gis_layer_id"));
		setMLParameter(res, rs);
		return res;
	}

	public static GisLayerFilter getGisLayerFilter(ResultSet rs)
			throws Exception {
		GisLayerFilter res = new GisLayerFilter();
		res.setId(rs.getInt("id"));
		res.setGis_layer_id(rs.getInt("gis_layer_id"));
		res.setTitle(rs.getString("title"));
		res.setFilter_text(rs.getString("filter_text"));
		res.setType_id(rs.getInt("type_id"));
		return res;
	}

	public static GisLayer getGisLayer(ResultSet rs) throws Exception {
		GisLayer res = new GisLayer();
		res.setId(rs.getInt("id"));
		res.setGismap_id(rs.getInt("gismap_id"));
		res.setLayername(rs.getString("layername"));
		res.setTitle(rs.getString("title"));
		res.setIswms(rs.getBoolean("iswms"));
		res.setPriority(rs.getInt("priority"));
		res.setBaselayer(rs.getBoolean("baselayer"));
		res.setGmaptype(rs.getInt("gmaptype"));
		res.setSphericalmercator(rs.getBoolean("sphericalmercator"));
		res.setWmsurl(rs.getString("wmsurl"));
		res.setShowinmap(rs.getBoolean("showinmap"));
		res.setForm_defination_xml(rs.getString("form_defination_xml"));
		res.setButton_icon(rs.getString("button_icon"));
		res.setButton_title(rs.getString("button_title"));
		res.setCan_delete(rs.getBoolean("can_delete"));
		return res;
	}
}
